function CMSantOnLoad(){

    if(SPEditMode){
        CMSantEditMode();
    }

}
 

function CMSantEditMode(){
    // Editor        
    // are we on a page that has an Editor?
    if($(".ms-formbody").contents("[nodeType=8][nodeValue*='FieldInternalName=\"CMSantBody\"']").length != 0){
                //var domain = getDomain(document.location.href);
                //CKEDITOR.basePath = domain + "/ckeditor/";
	            CKEDITOR.replace($(".ms-formbody").contents("[nodeType=8][nodeValue*='FieldInternalName=\"CMSantBody\"']").parent().find("textarea").attr("name"), {
                filebrowserBrowseUrl : CMSant.editor.pictureLib.path + '/Forms/AllItems.aspx',
                filebrowserUploadUrl : '/_layouts/Upload.aspx?List=%7BD8E052F6%2DCA18%2D49AC%2D9EB7%2D193CE524C92D%7D&RootFolder=' + CMSant.editor.pictureLib.name,
                customConfig : '/_catalogs/masterpage/CMSant/scripts/CKEditorConfig.js',
                contentsCss : '/_catalogs/masterpage/CMSant/styles/CKEditor.css',
                extraPlugins : '',
                removePlugins : 'save,newpage,smiley',
                width: '600',
                emailProtection : 'encode'
            });
            // are we on a content type that is a language folder?
            
    };
    // set the content type START
    $selContentType = $("select[id$='ContentTypeChoice'] option:selected");
    if($selContentType.length){
        setContentType($selContentType.val());
        // if we are on the language content type set autocomple on name and title
        if(CMSant.contentTypes.current == "languageFolder"){
            setNameAndTitleOnLanguageForm();
            // load the css for autocomplete
            loadCss("scripts/plugins/autocomplete/styles.css");
        }
    }
    // set the content type END
    

    
}



function setNameAndTitleOnLanguageForm(){
    //define arrays
    var suggestions = new Array();
    var data = new Array();
    //rebuild array
    $(CMSantGetLanguagesArr().items).each(function(i,val){
        data.push(val.data);
        suggestions.push(val.suggestions);
    });

    // get the fields
    $name =  $(".ms-formbody").contents("[nodeType=8][nodeValue*='FieldInternalName=\"FileLeafRef\"']").closest("tr").find("input");
    $title = $(".ms-formbody").contents("[nodeType=8][nodeValue*='FieldInternalName=\"Title\"']").closest("tr").find("input");
    if($title && $name){

        $.getScript(CMSant.trunk + "scripts/plugins/autocomplete/jquery.autocomplete-min.js",function(){
          $name.autocomplete({
                minChars:1,
                onSelect:function(val,data){
                    if($title.val() == ""){
                       $title.val(data) 
                    }
                },
                lookup:{suggestions:data,data:suggestions}
            }); 
          $title.autocomplete({
                minChars:1,
                onSelect:function(val,data){
                    if($name.val() == ""){
                       $name.val(data) 
                    }
                },
                lookup:{suggestions:suggestions,data:data}
            }); 
        });
    }
}


// will get the current content type from the value of the content type id
// ex: $selContentType = $("select[id$='ContentTypeChoice'] option:selected");
//    setContentType($selContentType.val()); sets CMSant.contentTypes.current to languageFolder
// TO DO: imporve by looping values in CMSant.contentTypes
function setContentType(val){
    ret = false;
    try {
        if(val.indexOf(CMSant.contentTypes.languageFolder + '00') == 0)
        {
            ret = "languageFolder";
        }
            else if(val.indexOf(CMSant.contentTypes.rootFolder + '00') == 0)
        {
            ret = "rootFolder";
        }
            else if(val.indexOf(CMSant.contentTypes.contentFolder + '00') == 0)
        {
            ret = "contentFolder";
        }
    }
    catch(err){
        // error logging
    }
    CMSant.contentTypes.current = ret;
}

// load css from trunk
// ex. scripts/plugins/autocomplete/styles.css
function loadCss(url){
  $(document.createElement('link')) 
    .attr({type: 'text/css', href:CMSant.trunk+'' + url, rel: 'stylesheet', media: 'screen'}) 
    .appendTo(document.getElementsByTagName('head')[0]);  
}



// get the domain from the url
// getDomain(document.location.href will return http://{domain}
function getDomain(url){
    var d = "";
    try {
        if(L_Menu_BaseUrl.length > 0){
            d = url.substr(0,url.indexOf(location.pathname));
        }
    }
    catch (e) { 
    }
    return d;
}

// /picture/chris.jpg will return chris.jpg
function getFileNameFromString(url){
    var fn = "";
    try {
        if(url.length > 0){
            fn = url.substr(url.search(/[0-9A-Za-z_ ]+(.[jJ][pP][gG]|.[gG][iI][fF])$/g),url.length); 
        }
    }
    catch (e) { 
    }
    return fn;
}

// will return a array ["/ic/chris.jpg", "chris.jpg", "/ic/_t/chris_jpg.jpg", "/ic/_w/chris_jpg.jpg"]
// {"orginal fileLeafRef","file name","thumbnail","middle size img"}
function getTumbnailsFromFile(fileLeafRef){
    var a = new Array();
    try {
        var fL = getFileNameFromString(fileLeafRef);
        a.push(fileLeafRef);
        a.push(fL);
        a.push(fileLeafRef.replace(fL,"_t/" + fL.replace(".","_") + ".jpg"));
        a.push(fileLeafRef.replace(fL,"_w/" + fL.replace(".","_") + ".jpg"));
    }
    catch (e) { 
        // error logging
    }
    return a;
}

/*
* get url queries
* return: url queries dictionary
*/
function getUrlQueries() {
    var srch = window.location.search;
    if (srch.length > 0) srch = srch.substring(1);
    var sep = srch.split('&');
    var queries = {};
    $.each(sep, function(idx, part) {
        var tmp = part.split('=');
        queries[tmp[0]] = decodeURIComponent(tmp[1]);
    });
    return queries;
}

/*
* go to url which in the ?Source= url query
* defaultTarget: if the source is null, where to go
*/
function goToSource(defaultTarget) {
    var queries = getUrlQueries();
    if (queries['Source'] == undefined) {
        if (defaultTarget != undefined) window.location = defaultTarget;
    } else window.location = queries['Source'];
}

// returns items {suggestions,data}
function CMSantGetLanguagesArr(){
    var languages = google.language.Languages;
    var langArr = {items: new Array()};
    for (l in google.language.Languages) {
        var res = {};
        res["suggestions"] = l;
        res["data"] = languages[l];
        langArr.items.push(res);
    }
    return langArr;
}


// call onload event
google.setOnLoadCallback(CMSantOnLoad);